package com.blog.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.base.controller.BaseController;
import com.blog.base.validator.InspectorException;
import com.blog.web.bean.Album;
import com.blog.web.bean.Article;
import com.blog.web.service.PictureService;

@Scope("prototype")
@Controller
@RequestMapping(value="/album")
public class AlbumController extends BaseController
{
	@Resource
	PictureService pictureService;
	/**
	 * @todo 打开添加相册的模态框
	 * @author fg
	 * @date 2016年4月14日
	 */
	@RequestMapping("preAddAlbum")
	public ModelAndView preAddAlbum() 
	{
		mav.setViewName("admin/picture/album_add_edit");
		return mav;
	}
	
	
	/**
	 * @todo 保存，新增相册
	 * @author fg
	 * @throws InspectorException 
	 * @date 2016年4月15日
	 */
	@RequestMapping("addOrSaveAlbum")
	@ResponseBody
	public JSONObject addOrSaveAlbum(@Valid Album album,BindingResult result) throws InspectorException 
	{
		if(result.hasErrors())
			throwInspectorException(result);
		try
		{
			Integer success =  pictureService.addOrSaveAlbum(album);
			json.put("msg", "保存成功！");
			json.put("success", success);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			json.put("msg", e.toString());
			json.put("success", 0);
		}
		
		return json;
	}
	
	/**
	 * @todo 修改相册展示
	 * @author fg
	 * @date 2016年4月16日
	 */
	@RequestMapping("preEditAlbum")
	public ModelAndView preEditAlbum(Integer id) 
	{
		mav.setViewName("admin/picture/album_add_edit");
		mav.addObject("id", pictureService.queryAlbumById(id));
		return mav;
	}
	
	/**
	 * @todo 查询相册
	 * @author fg
	 * @date 2016年4月16日
	 */
	@RequestMapping("queryAlbumByOption")
	public ModelAndView queryAlbumByOption() 
	{
		List<Album> albumList = pictureService.queryAlbumByOption(getRequestMap());
		mav.setViewName("admin/picture/album_list");
		mav.addObject("albumList", pictureService.queryAlbumByOption(getRequestMap()));
		return mav;
	}
	
}
